package org.jeecg.modules.sggg.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.sggg.entity.SgDrugRecord;
import org.jeecg.modules.sggg.entity.SgGuidelines;
import org.jeecg.modules.sggg.service.ISgDrugRecordService;
import org.jeecg.modules.sggg.service.ISgGuidelinesService;
import org.jeecg.modules.system.controller.SysRoleController;
import org.jeecg.modules.system.entity.SysDepart;
import org.jeecg.modules.system.entity.SysRole;
import org.jeecg.modules.system.entity.SysUser;
import org.jeecg.modules.system.service.ISysDepartService;
import org.jeecg.modules.system.service.ISysRoleService;
import org.jeecg.modules.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
 * @Description: 用药指导
 * @Author: jeecg-boot
 * @Date: 2022-10-17
 * @Version: V1.0
 */
@Api(tags = "用药指导")
@RestController
@RequestMapping("/sggg/sgGuidelines")
@Slf4j
public class SgGuidelinesController extends JeecgController<SgGuidelines, ISgGuidelinesService> {
    @Autowired
    private ISgGuidelinesService sgGuidelinesService;

    @Autowired
    private ISysRoleService sysRoleService;

    @Autowired
    private ISgDrugRecordService sgDrugRecordService;

    @Autowired
    private ISysDepartService sysDepartController;

    @Autowired
    private ISysUserService sysUserService;

    /**
     * 分页列表查询
     *
     * @param sgGuidelines 对象
     * @param pageNo       当前页
     * @param pageSize     页大小
     * @return 当前页集合
     */
    //@AutoLog(value = "用药指导-分页列表查询")
    @ApiOperation(value = "用药指导-分页列表查询", notes = "用药指导-分页列表查询")
    @GetMapping(value = "/list")
    public Result<IPage<SgGuidelines>> queryPageList(SgGuidelines sgGuidelines,
                                                     @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                     @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
        /*for(SysRole role : roleList){
            if(!role.getRoleCode().contains("admin")){
                code = "other";
            }else if(role.getRoleCode().contains("admin_")){
                code = "admin_";
            }else if(role.getRoleCode().contains("admin")){
                code = "admin";
                break;
            }

        }*/
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        //SysRole role = sysRoleController.getUserRole(sysUser.getId()).getResult();
        List<SysRole> roleList = sysRoleService.getUserRole(sysUser.getId());
        String code = "";
        StringBuilder sb = new StringBuilder();
        for (SysRole role : roleList) {
            sb.append(role.getRoleCode());
            sb.append(",");
        }
        String sbstr = sb.toString();
        if (sbstr.contains("admin") ) {
            code = "admin";
        } else if (sbstr.contains("dept_manage")) {
            //机构管理员
            code = "dept_manage";
        } else {
            code = "other";
        }

        SysDepart sysDepart = sysDepartController.getById(sysUser.getDepartIds());
        SysUser sysUser1 = sysUserService.getById(sysUser.getId());
        if(sysDepart !=null && sysDepart.getDeptAttr()!=null){
            // 1.三高之家
            if(sysDepart.getDeptAttr().equals("1")){
//                queryWrapper.eq("doctor_id", sysUser1.getId().toString());
                sysUser.setId(sysUser1.getId());
            }
            // 2.三高基地
            if(sysDepart.getDeptAttr().equals("2")){
                String[] array = sysUser1.getResponsibleAgency().split(",");
                List<String> responsibleAgencies = Arrays.asList(array);
                sgGuidelines.setResponsibleAgencies(responsibleAgencies);
//                queryWrapper.in("sys_org_code", responsibleAgencies);
            }
            // 3.三高中心
            if(sysDepart.getDeptAttr().equals("3")){
                String[] array = sysUser1.getResponsibleAgency().split(",");
                List<String> responsibleAgencies = Arrays.asList(array);
                sgGuidelines.setResponsibleAgencies(responsibleAgencies);
            }
        }else{
            sysUser.setId(sysUser1.getId());
        }


        sgGuidelines.setSysOrgCode(sysUser.getOrgCode());
        Page<SgGuidelines> page = new Page<>(pageNo, pageSize);
        IPage<SgGuidelines> pageList = sgGuidelinesService.pageList(page, sgGuidelines,
                code,
                sysUser.getId());
        return Result.OK(pageList);
    }

    /**
     * xl ---- 居民信息管理 药物治疗方案
     *
     * @param mid 居民id
     * @return 数据
     */
    @GetMapping(value = "/queryListByMid")
    public Result<List<SgGuidelines>> queryListByMid(String mid) {
        return Result.OK(sgGuidelinesService.list(
                new QueryWrapper<SgGuidelines>().eq("member_id", mid)
        ));
    }

    @AutoLog(value = "用药指导-添加")
    @ApiOperation(value = "用药指导-添加", notes = "用药指导-添加")
    @PostMapping(value = "/add")
    public Result<String> add(@RequestBody SgGuidelines sgGuidelines) {
        sgGuidelinesService.save(sgGuidelines);
        return Result.OK("添加成功！");
    }

    /**
     * 小程序添加用药指导
     *
     * @param sgGuidelines
     * @return
     */
    @PostMapping(value = "/addApplets")
    public Result<String> addApplets(@RequestBody SgGuidelines sgGuidelines) {
        SgDrugRecord sgDrugRecord = sgDrugRecordService.getById(sgGuidelines.getName());
        sgGuidelines.setName(sgDrugRecord.getName());
        sgGuidelinesService.save(sgGuidelines);
        return Result.OK("添加成功！");
    }

    @AutoLog(value = "用药指导-编辑")
    @ApiOperation(value = "用药指导-编辑", notes = "用药指导-编辑")
    @RequestMapping(value = "/edit", method = {RequestMethod.PUT, RequestMethod.POST})
    public Result<String> edit(@RequestBody SgGuidelines sgGuidelines) {
        sgGuidelinesService.updateById(sgGuidelines);
        return Result.OK("编辑成功!");
    }

    @AutoLog(value = "用药指导-通过id删除")
    @ApiOperation(value = "用药指导-通过id删除", notes = "用药指导-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<String> delete(@RequestParam(name = "id") String id) {
        sgGuidelinesService.removeById(id);
        return Result.OK("删除成功!");
    }

    @AutoLog(value = "用药指导-批量删除")
    @ApiOperation(value = "用药指导-批量删除", notes = "用药指导-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<String> deleteBatch(@RequestParam(name = "ids") String ids) {
        this.sgGuidelinesService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功!");
    }

    @ApiOperation(value = "用药指导-通过id查询", notes = "用药指导-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<SgGuidelines> queryById(@RequestParam(name = "id") String id) {
        SgGuidelines sgGuidelines = sgGuidelinesService.getById(id);
        if (sgGuidelines == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(sgGuidelines);
    }

    /**
     * 导出excel
     */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, SgGuidelines sgGuidelines) {
        return super.exportXls(request, sgGuidelines, SgGuidelines.class, "用药指导");
    }

    /**
     * 通过excel导入数据
     */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, SgGuidelines.class);
    }

}
